//图片预览
$(document).ready(function () {
      $(".fancybox-thumb").fancybox({
        prevEffect: 'elastic',
        nextEffect: 'elastic',
        helpers: {
          title: {
            type: 'inside'
          },
          thumbs: {
            width: 50,
            height: 50
          }
        }
      });
    });
$(function () {
    $("#jqGrid").jqGrid({
        url: '../imghouse/list',
        datatype: "json",
        colModel: [			
			//{ label: 'id', name: 'id', width: 50, key: true },
        	{ label: '图片', name: 'picUrl', width: 80 ,hidden : true}, 			
        	{ label: '图片基础路径', name: 'image_base_path', width: 80 ,hidden : true}, 			
        	{
                label: '图片预览', name: '', index: 'operate', width: 100, align: 'center',
                formatter: function (cellvalue, options, rowObject) {
                	var uri=rowObject.image_base_path+"/"+rowObject.picUrl;
                	console.log(uri);
                	var alt=rowObject.cname;
                    var detail="<a class='fancybox-thumb' rel='fancybox-thumb' href='"+uri+"' title='"+alt+"'><img width='100' height='50' src='"+uri+"' alt='"+alt+"'' /></a>";
                    return detail;
                }
            },
			//{ label: '渠道标识', name: 'channelid', width: 80 }, 			
			{ label: '图片名称', name: 'cname', width: 60 }, 			
			{ label: '图片描述', name: 'des', width: 80 }, 			
			{ label: '创建人', name: 'createuser', width: 50 }, 			
			{ label: '创建时间', name: 'createtime', width: 100 }, 			
			{ label: '修改人', name: 'updateuser', width: 50 }, 			
			{ label: '修改时间', name: 'updatetime', width: 100 },
			{
                label: '复制图片地址', name: '', index: 'operate', width: 50, align: 'center',
                formatter: function (cellvalue, options, rowObject) {
                    var detail="<a class='btn btn-default' onclick='copyTextToClipboard(\""+rowObject.picUrl+"\")'><i class='fa fa-files-o'></i>&nbsp;复制</a>";
                    return detail;
                }
            }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
//图片上传转换
function fileChange(num, e) {
    //压缩后的图片
    lrz(e.files[0], {quality:1}).then(function (rst) {
    	vm.imgHouse.imgBase64=rst.base64;
    });
}
var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{cname:null},
		showList: true,
		showImg:true,
		title: null,
		imgHouse: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.showImg = true;
			vm.title = "新增";
			vm.imgHouse = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
			vm.showImg = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.imgHouse.id == null ? "../imghouse/save" : "../imghouse/update";
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.imgHouse),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../imghouse/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../imghouse/info/"+id, function(r){
                vm.imgHouse = r.imgHouse;
                $("#imgId").attr("src", r.imgHouse.image_base_path+"/"+r.imgHouse.picUrl); 
            });
		},
		reload: function (event) {
			//清空表单，否则图片上传默认展示上一次的结果
			$('#form1')[0].reset();
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{
					'cname': vm.q.cname
				},
                page:page
            }).trigger("reloadGrid");
		}
	}
});